/**
 * OSAmI: Open Source Ambient Intelligence
 * A simple publish/subscribe/dispatch API (based on multicast communications) for constrained devices.
 * Copyright (C) Lionel Debroux 2006 (as a TER/Magistere M1 student in the LSR-IMAG-ADELE team, for a demo in the H-Omega project)
 * Copyright (C) Lionel Debroux 2010 (as an engineer in the LCIS-INPG-CTSYS team, for a pre-demo in the FR part of the OSAMI project)
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
 */

package fr.imag.adele.homega.impl.unicast;

import java.net.InetAddress;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * This class maintains the state of subscriptions for a given address and port
 */
public final class TopicRegistrationsInfo implements Comparable {
	private InetAddress topic;
	private int port;

	// Object representing listeners, we'll use it for receive() method.
	private java.util.TreeSet<UnicastReceiver> listeners;

	public TopicRegistrationsInfo(InetAddress topic, int port) {
		this.topic = topic;
		this.port = port;
		this.listeners = new TreeSet<UnicastReceiver>();
	}

	public void addListener(UnicastReceiver listener) {
		listeners.add(listener);
	}

	public void removeListener(UnicastReceiver listener) {
		listeners.remove(listener);
	}

	/*public InetAddress getInetAddress() {
		return topic;
	}

	public void setTopic(InetAddress topic) {
		this.topic = topic;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}*/

	public boolean isEmpty() {
		return listeners.isEmpty();
	}

	// Used when dispatching messages. 
	public Iterator<UnicastReceiver> getIterator() {
		return listeners.iterator();
	}

	public String toString() {
		return listeners.toString();
	}

	public int compareTo(Object o) {
		if (o instanceof TopicRegistrationsInfo) {
			if (   ((TopicRegistrationsInfo)o).topic == this.topic
				&& ((TopicRegistrationsInfo)o).port == this.port) {
				return 0;
			}
		}
		return 1;
	}
}
